<extend name="layout/edit" />

<block name="HEADER">
	<style type="text/css">.card_type .layui-form-select{ width: 80px; }</style>
	<script charset="utf-8" src="https://map.qq.com/api/js?v=2.exp&key=FZZBZ-APGK6-6CJSU-EXA6S-P6RN7-MBBIX"></script>
</block>

<block name="CONTENT">
    <control:form id="form"/>
</block>

<block name="FOOTER">
    <script type="text/javascript">
   	
   	var shop_id = <php>echo $userInfo['shop_id'];</php>;
   	
    var  page=(function(ns){
    	
		ns.init = function(){
			if(shop_id>0 && __mode != 3){
        		form.setReadonly("user_name",false,false);
			}
			if(shop_id==0 && form.getValue('id') == 0){
				form.setReadonly("user_name",false,false);
        	}
			
			if(__mode == 3) $("#btnSave").remove();
			$("#addressDetailed").parents('.table-cell').css('width','50%');
			$("#idcard").parents('.table-input').css('display','flex').addClass('card_type').prepend('<select form-datatype="select" onclick="" name="card_type" type="select" value="1" datatype="*" ignore="" errormsg="格式不正确" edit="true"  style="width: 80px;" '+ (shop_id ? 'disabled' : '')+'><option value="0" selected>身份证</option><option value="1">护照</option><option value="2">驾驶证</option></select>');
        	form._initEvent();
        	ns.initMap();
        
		}
		
		//选择时间
		ns.selectTime=function(){
			var hours = form.getValue('business_hours').split(" - ");
			form.setValue('service_start_time',hours[0]);
			form.setValue('service_end_time',hours[1]);
		}
		
		//选择用户
    	ns.selectUser = function(){
    		if(shop_id>0){ return false; }
    		if(shop_id==0 && form.getValue('id') == 0){ return false;  }
    		var filter = {
	            url:$.getModule('selectUser'),
	            mode:1,
	            user_ids:form.getValue("uid")
	        }
	        $.openIframe(filter, "选择会员",['80%', '85%']);
    	}
    	
    	//选择商户的回调
    	ns.selectUserCallback = function(ids,data){
    		form.setValue('uid',ids);
    		form.setValue('user_name',data[0].name);
    	}
		
		//初始化地图
		ns.initMap = function (){
			var map = new qq.maps.Map(document.getElementById('addressDetailed'),{ zoom: 13  });
		    //调用城市服务信息
		    var citylocationMarker = null;
		    var citylocation = new qq.maps.CityService({
		        complete : function(results){
		            map.setCenter(results.detail.latLng);
		            //设置marker标记
		            if(citylocationMarker!=null)  citylocationMarker.setMap(null);
		           	citylocationMarker = new qq.maps.Marker({
		                map: map,
		                position: results.detail.latLng
		            });
		            qq.maps.event.addListener(map, 'click', function(event) {
			            citylocationMarker.setMap(null);      
			    	});
		    		var latLng = 	results.detail.latLng;
		    		ns.getLocation(latLng);
		        }
		    });
		    
		    //调用地址解析类
		    var geocoderMarker = null;
		    var geocoder = new qq.maps.Geocoder({
		        complete : function(result){
		            map.setCenter(result.detail.location);
		            if(geocoderMarker!=null)  geocoderMarker.setMap(null);
		            if(citylocationMarker!=null)  citylocationMarker.setMap(null);
		           geocoderMarker = new qq.maps.Marker({
		                map:map,
		                position: result.detail.location
		            });
		            qq.maps.event.addListener(map, 'click', function(event) {
			            geocoderMarker.setMap(null);
			    	});
			    	ns.getLocation(result.detail.location);
		        }
		    });
		   
		    //添加监听事件   获取鼠标单击事件
		    qq.maps.event.addListener(map, 'click', function(event) {
		    	var marker=new qq.maps.Marker({
		            position:event.latLng, 
		            map:map
		       	}); 
		       	qq.maps.event.addListener(map, 'click', function(event) {
		            marker.setMap(null);      
		    	});
		    	ns.getLocation(event.latLng);
		    });
		    
		    //调用searchLocalCity();方法    根据用户IP查询城市信息。
		    if(form.getValue('latitude')){
		    	//获取经纬度数值   按照,分割字符串 取出前两位 解析成浮点数
			    var latLng = new qq.maps.LatLng( form.getValue('latitude') , form.getValue('longitude'));
			    geocoder.getAddress(latLng);
		    }else{
		    	citylocation.searchLocalCity();
		    }
		    
		    //监听地区
		    form.on("input",'region',function(res){
		    	geocoder.getLocation( form.getValue("region").replace(/-/g,',') );
		    });
		}
		
		//获取地址
		ns.getLocation = function(latLng,index){
			form.setValue("local",latLng.getLat()+","+latLng.getLng());
			form.setValue("latitude",latLng.getLat());
			form.setValue("longitude",latLng.getLng());
			$.ajax({
		    	url:"http://apis.map.qq.com/ws/geocoder/v1/?",
		        type:"get",
		        dataType:'jsonp',
		        data:{
　　　　　　 location:latLng.lat+","+latLng.lng,
　　　　　　 key:"FZZBZ-APGK6-6CJSU-EXA6S-P6RN7-MBBIX",
					output:"jsonp"
　　　　	},
				async:false,      //同步
		        jsonp:"callback",
		        jsonpCallback:"QQmap",
		        success:function(res){
		        	var result = res.result;
					if(res.status != 0 ){
						!index ? ns.getLocation(latLng,1) : "";
						return false;
					}
					form.setValue("address_detail", result.formatted_addresses.rough );
					form.setValue("region", result.ad_info.name.replace("中国,","") );
		        },
		        error : function(err){ alert("服务端错误，请刷新浏览器后重试") }
			})
		}
		
        //保存
        ns.save=function(){
        	form.setValue("is_del",'0');
        	form.save(1).then(function(res){
        		window.__user_guid__
            	form.opener.grid.load();
            });
        	return false;
        	var data = form.getData();
        	if(!data) return false;
        	$.ajaxs('/admin/v1/shop/add',data,function(res){
        		var data =  res.data;
				if(data && __mode == 1){}
	    		form.save(1).then(function(res){
	            	form.opener.grid.load();
	            });
	    	});
        }
        
        return ns;
    }({}));
    
    $(function(){ page.init(); });
   </script>
</block>



